﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormNhomThueHangHoa : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormNhomThueHangHoa()
        {
            InitializeComponent();
        }
        private int idtam = 0;
        private void FormNhomThueHangHoa_Load(object sender, EventArgs e)
        {
            GetListNhomThueHangHoa();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var t = new FormNhomThueHangHoaThem();
            t.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var t = new FormNhomThueHangHoaChinhSua();
            t.id = idtam;
            t.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListNhomThueHangHoa();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvNhomThueHangHoa_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvNhomThueHangHoa.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }

        private void dgvNhomThueHangHoa_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var t = new FormNhomThueHangHoaChinhSua();
            t.id = idtam;
            t.ShowDialog();
        }

        void GetListNhomThueHangHoa()
        {
            dgvNhomThueHangHoa.DataSource = db.NhomThueHangHoas.ToList();
            Reset();
        }
        void Reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        void Delete()
        {
            var t = db.NhomThueHangHoas.Find(idtam);
            if (
                MessageBox.Show("Bạn muốn xóa nhóm thuế: " + t.TenThue + "!", "Xác nhận", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    db.NhomThueHangHoas.Remove(t);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }

            }
        }
    }
}
